package com.xywl.escort.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.xywl.escort.R;

/**
 * Created by 32422 on 2017/10/14.
 */

public class CustomDialog2 extends Dialog {
    public CustomDialog2(Context context) {
        super(context);
    }

    public CustomDialog2(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {

        private Context context;
        private CharSequence titleText;
        private CharSequence messageText;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(CharSequence sequence) {
            this.titleText = sequence;
            return this;
        }

        public Builder setTitle(int strId) {
            this.titleText = context.getText(strId);
            return this;
        }

        public Builder setMessage(CharSequence sequence) {
            this.messageText = sequence;
            return this;
        }

        public Builder setMessage(int strId) {
            this.messageText = context.getText(strId);
            return this;
        }

        public CustomDialog2 create() {
            final CustomDialog2 dialog = new CustomDialog2(context, R.style.CustomDialogStyle);
            LayoutInflater inflater = LayoutInflater.from(context);
            View contentView = inflater.inflate(R.layout.custom_dialog2, null);
            dialog.addContentView(contentView, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            TextView titleView = contentView.findViewById(R.id.tv_dialogTitle);
            TextView messageView = contentView.findViewById(R.id.tv_dialogMsg);
            ImageView imgClose = contentView.findViewById(R.id.img_close);

            // 赋值和添加事件
            if (TextUtils.isEmpty(titleText)) {
                ((View) titleView.getParent()).setVisibility(View.GONE);
            } else {
                ((View) titleView.getParent()).setVisibility(View.VISIBLE);
                titleView.setText(titleText);
            }

            if (TextUtils.isEmpty(messageText)) {
                messageView.setVisibility(View.GONE);
            } else {
                messageView.setVisibility(View.VISIBLE);
                messageView.setText(messageText);
            }

            imgClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.setContentView(contentView);
            return dialog;
        }
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        if (null != window) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setAttributes(layoutParams);
        }
    }
}
